﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web;
using System.Globalization;
using BlogEngine.Core;

namespace Controls
{
    /// <summary>
    /// BulletedList control that generates list of active languages
    /// and handles the selected language.
    /// </summary>
    public class LanguageSelector : BulletedList
    {
        /// <summary>
        /// PreLoads list of active languages
        /// </summary>
        /// <param name="sender">Control object</param>
        /// <param name="e">Event arguments</param>
        protected override void OnPagePreLoad(object sender, EventArgs e)
        {
            base.OnPagePreLoad(sender, e);
            bindLanguages();
        }

        private string _selectedLanguageText;
        /// <summary>
        /// Gets and sets friendly text with a piece of information
        /// about currently selected language.
        /// </summary>
        public string SelectedLanguageText
        {
            get { return _selectedLanguageText; }
            set { _selectedLanguageText = value; }
        }

        private bool _useViewState;
        /// <summary>
        /// Gets and sets whether to retrieve selected culture from ViewState or QueryString.
        /// </summary>
        public bool UseViewState
        {
            get { return _useViewState; }
            set { _useViewState = value; }
        }

        /// <summary>
        /// Gets and sets currently selected culture.
        /// </summary>
        public string SelectedCulture
        {
            get
            {
                if (UseViewState && ViewState["SelectedCulture"] != null)
                    return ViewState["SelectedCulture"].ToString();
                else
                {
                    if (!String.IsNullOrEmpty(HttpContext.Current.Request.QueryString["culture"]) && BlogSettings.Instance.CultureFrontend[HttpContext.Current.Request.QueryString["culture"]] != null)
                        return HttpContext.Current.Request.QueryString["culture"];
                }

                if (Items.Count > 0)
                    return Items[0].Value;
                else
                    return "";
            }
            set
            {
                if (UseViewState)
                {
                    ViewState["SelectedCulture"] = value;
                    SelectedLanguageText = Items.FindByValue(value).Text;
                }
                else
                    HttpContext.Current.Response.Redirect(HttpContext.Current.Request.FilePath + "?id=" + HttpContext.Current.Request.QueryString["id"] + "&culture=" + value);
            }
        }

        /// <summary>
        /// Loads the list of active languages to BulletedList control
        /// </summary>
        private void bindLanguages()
        {
            if (Items.Count == 0)
            {
                foreach (CultureCollectionItem culture in BlogSettings.Instance.CultureFrontend.FindAll(delegate(CultureCollectionItem item) { return item.Active; }))
                    Items.Add(new ListItem(CultureInfo.CreateSpecificCulture(culture.CultureName).NativeName, culture.CultureName));
            }

            SelectedLanguageText = Items.FindByValue(SelectedCulture).Text;
        }

        /// <summary>
        /// Overrides OnClick event method to set newly selected culture.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClick(BulletedListEventArgs e)
        {
            SelectedCulture = Items[e.Index].Value;
            base.OnClick(e);
        }
    }
}
